package blazingbubble.network.lobby.strategy;

import java.util.Collection;

import org.apache.log4j.Logger;

import blazingbubble.graphics.lobby.IGameListUpdateListener;

import moltencore.network.protocol.GameListGameData;
import spark.network.protocol.Acknowledgement;
import spark.network.protocol.Response;
import spark.network.strategy.IProtocolHandleStrategy;
import spark.network.protocol.Base;
import spark.utility.OutWrapper;

public class GameListGameDataStrategy implements IProtocolHandleStrategy<GameListGameData> {

	private Logger logger = Logger.getLogger(GameListGameDataStrategy.class);
	private IGameListUpdateListener updateListener;

	@Override
	public void handle(GameListGameData packet, OutWrapper<Response> acknowledgement, Collection<Base> additionalData) {
		if (updateListener.isReceiving()){
			logger.debug("GameListGameData packet: " + packet.getPacketId() + " ACCEPTED, adding game data to table.");
			updateListener.addGame(
					packet.getPort(),
					packet.getCurrentPlayerCount(),
					packet.getMaximumPlayerCount(),
					packet.getNormalBubbleCount(),
					packet.getGhostBubbles(),
					packet.getRainbowBubbles(),
					packet.getBlazingBubbles(),
					packet.getChainReactions(),
					packet.getGravity(),
					packet.getWind(),
					packet.getFieldFlipped(),
					packet.getGameMode(),
					packet.getTeamsEnabled(),
					new String(packet.getGameName()));
		}
		else {
			logger.debug("GameListGameData packet: " + packet.getPacketId() + " REJECTED, client is not in receiving mode.");
		}
		acknowledgement.value = new Acknowledgement(packet);
	}

	public void setGameListUpdateListener(IGameListUpdateListener updateListener){
		this.updateListener = updateListener;
	}
}
